import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class Demo2 {
    public static void main(String[] args) {
        System.out.println("start...");
        //假设数组中数据为以下数组
        List<Integer> list = Arrays.asList(10, 1, 1, 2, 3, 4, 6, 7, 8, 1);
        List<Integer> merge = merge(list);
        System.out.println("max:" + merge.get(merge.size()-1));
        System.out.println("end...");
    }

    private static List<Integer> merge(List<Integer> list) {
        //先排序
        List<Integer> sList = list.stream().sorted().collect(Collectors.toList());
        //再合并
        boolean merge = false;
        int end = sList.size();
        for (int i = 1; i < end; i++) {
            if (Objects.equals(sList.get(i - 1), sList.get(i))) {
                int newVal = sList.get(i - 1) + sList.get(i);
                System.out.println("newVal:" + newVal);
                sList.set(i, newVal);
                end--;
                merge = true;
                sList.remove(i - 1);
            }
        }
        //递归
        if (merge) {
            return merge(sList);
        } else {
            return sList;
        }

    }
//
//
//    private int x2(int a, int b) {
//        if (a == b) {
//            return a + b;
//        } else {
//            return 0;
//        }
//    }
//
//    private int max(int a, int b) {
//        return Math.max(a, b);
//    }


}
